Skip to content

Add x402 BCH payment support for HTTP requests#1

Merged
joemarct merged 13 commits intomasterfrom
feature/x402-support
Mar 29, 2026
Merged

Add x402 BCH payment support for HTTP requests#1
joemarct merged 13 commits intomasterfrom
feature/x402-support

Conversation

@joemarct
Copy link
Copy Markdown
Member

@joemarct joemarct commented Mar 29, 2026

Summary

  • Add paytaca pay command for making HTTP requests to x402-enabled APIs
  • Add paytaca check command to preview payment requirements before spending
  • Add paytaca opencode and paytaca claude commands to install x402 skill for AI agents
  • Implement x402-bch v2.2 specification with BCH payment signing and verification
  • Add reference x402 server for local testing

Commands Added

Command Description
paytaca check <url> Check if URL requires x402 payment
paytaca pay <url> Make paid HTTP request (auto-handles 402)
paytaca opencode Install x402 skill for OpenCode AI agents
paytaca claude Install x402 skill for Claude Code agents

Key Features

  • AI-friendly output: --json flag for machine-readable responses
  • Dry-run mode: --dry-run to preview payments without executing
  • User approval: Explicit approval required before any BCH is spent
  • x402-bch v2.2: Full compliance with latest specification (https://github.com/x402-bch/x402-bch)

Files Changed

  • src/commands/pay.ts - Main payment handler
  • src/commands/check.ts - Payment check/preview
  • src/commands/opencode.ts, src/commands/claude.ts - Skill installation
  • src/wallet/x402.ts - Payment signing and verification
  • src/utils/x402.ts - Header parsing, payment requirement selection
  • src/types/x402.ts - Type definitions
  • x402-server/ - Reference server implementation
  • skills/paytaca/SKILL.md - AI agent skill instructions

joemarct added 11 commits March 29, 2026 11:42
- Add pay command: paytaca pay <url> for HTTP requests with x402 payment
- Add X402Payer class integrating with LibauthHDWallet for signing
- Add x402 types (PaymentRequired, PaymentPayload, Authorization, etc.)
- Add utility functions for header parsing and payload building
- Support BCH mainnet and chipnet via bip122 CAIP-2 network format
- Uses bitcoinjs-message for BCH message signing (same as bch-js)
- Payment flow: request -> 402 -> parse headers -> broadcast BCH tx -> retry with signature
- Add --dry-run flag to pay command for previewing payment without executing
- Add --json flag for machine-readable output (useful for AI agents)
- Add new 'check' command to pre-verify x402/BCH support before paying
- Refactor pay command into separate handlers for human/dry-run/JSON output
- JSON output includes success status, payment txid, response data, and errors
- Add 'paytaca opencode' command for managing opencode skill
- Supports: install, uninstall, status actions
- Installs skill to ~/.config/opencode/skills/paytaca/
- Skill teaches opencode to use paytaca check/pay for 402 handling
- Include skills/ directory in npm package distribution
- Fix address extraction in pay.ts to properly parse CashAddress format
  from payment URLs like 'bch:bitcoincash:qp...' extracting 'bitcoincash:qp...'
- Fix payload field in Authorization to contain stringified JSON instead of
  signature, allowing server to access payment recipients
- Fix x402-server to parse payload as JSON string instead of base64
- Fix payment URL parsing to not double-prefix with 'bch:' when address already has bitcoincash: prefix
- Add --payer option to allow custom payer identifier (e.g., user ID for server-side lookups) instead of always using wallet address
- Add recipient address output after successful payment
- Add payment request payload logging on x402 server for debugging
- Replace HTTP header-based 402 responses with JSON body format
- Use PAYMENT-SIGNATURE header with JSON-serialized PaymentPayload
- Implement BCH network validation (bip122 CAIP-2 format)
- Use libauth secp256k1 for message signing instead of bitcoinjs-message
- Add network parameter to selectBchPaymentRequirements for validation
- Update server to return v2.2 compliant PaymentRequired JSON
- Update pay and check commands for new x402 format
@opencode-agent
Copy link
Copy Markdown

Code Review Summary

Positive Aspects

  1. Well-organized architecture - Clean separation between commands, wallet logic, utilities, and types
  2. Good documentation - Comprehensive README, SKILL.md for AI agents, and clear JSDoc comments
  3. User-friendly features - --json flag for AI agents, --dry-run mode, human-readable output with chalk
  4. Security awareness - Emphasizes user approval in documentation, uses OS keychain for seed storage
  5. Reference server included - Full x402 server implementation for testing
  6. Type safety - Well-defined TypeScript interfaces for all x402 structures

⚠️ Issues Found

1. Critical: Missing User Confirmation (Security Issue)

  • pay.ts:142-143 - The pay command executes payments immediately without prompting for user confirmation
  • Documentation states "User approval required before any BCH is spent" but this isn't enforced in code
  • Fix: Add a confirmation prompt before executePay() sends transactions

2. Unused Imports

  • pay.ts:22 - signMessageBCH is imported but never used (it's used in wallet/x402.ts instead)
  • wallet/x402.ts:15 - parsePaymentResponse imported but never used

3. Minor Issues

  • opencode.ts:40-59 - Claude command is defined inside opencode.ts, should be in its own file per the PR description
  • utils/x402.ts:126-140 - signMessageBCH function duplicates message signing logic that may exist elsewhere
  • Missing error handling for network timeouts in payment flows
  • No tests included for the new functionality

📋 Recommendations

  1. Add user confirmation prompt to pay.ts before executing transactions
  2. Remove unused imports to clean up code
  3. Add unit tests for x402 parsing and payment logic
  4. Consider adding retry logic for transient network failures
  5. Add transaction timeout handling in executePay()

🎯 Overall Assessment

The PR implements x402 BCH payment support comprehensively and follows good coding practices. However, the missing user confirmation is a security concern that should be addressed before merging. The code is otherwise well-structured and documented.

New%20session%20-%202026-03-29T15%3A15%3A20.185Z
opencode session  |  github run

- Add payment confirmation prompt in pay command before executing BCH transactions
- Remove unused imports (signMessageBCH from pay.ts, parsePaymentResponse from wallet/x402.ts)
- Extract shared skill utilities to src/utils/skill.ts
- Move Claude command to its own file for extensibility
- Add SUPPORTED_ASSISTANTS array for easy AI assistant integration
@opencode-agent
Copy link
Copy Markdown

Based on my review of the code, here's my assessment:

Positive Aspects

  1. User confirmation is properly implemented - Contrary to the earlier review, pay.ts:385-393 has a confirmation prompt (promptConfirmation('Confirm payment?')) before executing any payment. This is good security practice.

  2. Clean architecture - Well-organized with commands, wallet logic, utilities, and types properly separated.

  3. Comprehensive documentation - Excellent README updates and SKILL.md for AI agent integration.

  4. Reference server included - Full x402 server implementation for testing (x402-server/).

  5. Good type safety - Well-defined TypeScript interfaces for all x402 structures.

⚠️ Minor Issues

  1. Unused imports (cleanup needed):

    • pay.ts:22 - signMessageBCH imported but unused (used in wallet/x402.ts instead)
    • wallet/x402.ts:15 - parsePaymentResponse imported but never used
  2. No tests - Project has no test framework set up. Consider adding tests for x402 parsing and payment logic in future.

📋 Recommendations

  1. Remove unused imports to clean up the code
  2. Add package-lock.json to .gitignore if it's being modified unintentionally
  3. Consider adding a test framework and basic tests for x402 functionality

🎯 Overall Assessment

Approve with minor cleanup. The PR is well-structured, secure (user confirmation works), and follows good practices. The x402 BCH payment implementation is complete and ready for use. Just clean up the unused imports before merging.

New%20session%20-%202026-03-29T15%3A31%3A02.005Z
opencode session  |  github run

- Add Vitest and @vitest/ui as dev dependencies
- Add test and test:watch scripts to package.json
- Add vitest.config.ts configuration
- Add x402.test.ts with 20 tests covering:
  - PaymentRequired JSON parsing
  - BCH payment requirement selection
  - PaymentPayload and Authorization building
  - PaymentResponse parsing
  - Network helper functions
@opencode-agent
Copy link
Copy Markdown

Based on my comprehensive review of the PR, here's my assessment:

Approve

Security Confirmation: User confirmation IS properly implemented at src/commands/pay.ts:385-393 with promptConfirmation('Confirm payment?'). The initial review concern was unfounded - the code clearly prompts for user approval before executing any payment.

Architecture: Clean separation of concerns:

  • Commands properly isolated in src/commands/
  • Wallet logic separated from CLI interface
  • Utility functions testable and well-organized
  • Type definitions comprehensive

Testing: Tests ARE included in src/utils/x402.test.ts with good coverage of parsing and utility functions. Vitest is configured properly.

Documentation: Excellent quality:

  • README clearly explains all x402 commands with examples
  • SKILL.md provides complete workflow guidance for AI agents
  • Inline comments reference the x402-bch v2.2 specification
  • x402-server is fully documented

AI Integration: Well-designed features:

  • --json flag for machine-readable output
  • --dry-run for safe preview
  • Skill installation commands work correctly
  • SKILL.md emphasizes user approval requirements

Minor cleanup needed:

  • src/utils/x402.ts:15 - parsePaymentResponse appears unused
  • Some unused imports flagged in earlier reviews (minor impact)

Overall: This is a well-implemented, secure, and thoroughly documented PR. The x402 BCH payment functionality is complete and production-ready.

New%20session%20-%202026-03-29T15%3A38%3A39.824Z
opencode session  |  github run

@joemarct joemarct merged commit b33d6d6 into master Mar 29, 2026
1 check passed
@joemarct joemarct deleted the feature/x402-support branch March 29, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant